C traps and pitfalls
نویسنده
چکیده
The C language is like a carving knife: simple, sharp, and extremely useful in skilled hands. Like any sharp tool, C can injure people who don’t know how to handle it. This paper shows some of the ways C can injure the unwary, and how to avoid injury. 0. Introduction The C language and its typical implementations are designed to be used easily by experts. The language is terse and expressive. There are few restrictions to keep the user from blundering. A user who has blundered is often rewarded by an effect that is not obviously related to the cause. In this paper, we will look at some of these unexpected rewards. Because they are unexpected, it may well be impossible to classify them completely. Nevertheless, we have made a rough effort to do so by looking at what has to happen in order to run a C program. We assume the reader has at least a passing acquaintance with the C language. Section 1 looks at problems that occur while the program is being broken into tokens. Section 2 follows the program as the compiler groups its tokens into declarations, expressions, and statements. Section 3 recognizes that a C program is often made out of several parts that are compiled separately and bound together. Section 4 deals with misconceptions of meaning: things that happen while the program is actually running. Section 5 examines the relationship between our programs and the library routines they use. In section 6 we note that the program we write is not really the program we run; the preprocessor has gotten at it first. Finally, section 7 discusses portability problems: reasons a program might run on one implementation and not another. 1. Lexical Pitfalls The first part of a compiler is usually called a lexical analyzer. This looks at the sequence of characters that make up the program and breaks them up into tokens. A token is a sequence of one or more characters that have a (relatively) uniform meaning in the language being compiled. In C, for instance, the token -> has a meaning that is quite distinct from that of either of the characters that make it up, and that is independent of the context in which the -> appears. For another example, consider the statement: if (x > big) big = x; Each non-blank character in this statement is a separate token, except for the keyword if and the two instances of the identifier big. In fact, C programs are broken into tokens twice. First the preprocessor reads the program. It must tokenize the program so that it can find the identifiers, some of which may represent macros. It must then replace each macro invocation by the result of evaluating that macro. Finally, the result of the macro replacement is reassembled into a character stream which is given to the compiler proper. The compiler then breaks the stream into tokens a second time. __________________ * This paper, greatly expanded, is the basis for the book C Traps and Pitfalls (Addison-Wesley, 1989, ISBN 0–201–17928–8); interested readers may wish to refer there as well. In this section, we will explore some common misunderstandings about the meanings of tokens and the relationship between tokens and the characters that make them up. We will talk about the preprocessor later. 1.1. = is not == Programming languages derived from Algol, such as Pascal and Ada, use := for assignment and = for comparison. C, on the other hand, uses = for assignment and == for comparison. This is because assignment is more frequent than comparison, so the more common meaning is given to the shorter symbol. Moreover, C treats assignment as an operator, so that multiple assignments (such as a=b=c) can be written easily and assignments can be embedded in larger expressions. This convenience causes a potential problem: one can inadvertently write an assignment where one intended a comparison. Thus, this statement, which looks like it is checking whether x is equal to y:
منابع مشابه
Pitfall Trapping of Microtus townsendii
-A field population of Microtus townsendii was trapped concurrently with live-traps and pitfall traps throughout the spring and summer of 1975. The population was at peak densities, with a maximum of 519 voles per acre. The number enumerated by pitfalls was up to two times larger than the number enumerated by live-traps. Capture of a large number of young in pitfalls indicated that mortality wa...
متن کاملEfficiency of small mammal trapping in an Atlantic Forest fragmented landscape: the effects of trap type and position, seasonality and habitat.
Trapping methods can strongly influence the sampling of mammal communities. This study compared the efficiency of the capture of small mammals in Sherman traps in two positions (at ground level and in trees) and pitfall traps in a fragmented landscape. Trapping sessions were carried out between October 2008 and October 2009 at two fragments (8 and 17 ha), an agroforest corridor between them, an...
متن کاملTrapping and marking terrestrial mammals for research: integrating ethics, performance criteria, techniques, and common sense.
We propose that researchers integrate ethics, performance criteria, techniques, and common sense when developing research trapping programs and in which members of institutional animal care and use committees address these topics when evaluating research protocols. To ask questions about ethics is in the best tradition of science, and researchers must be familiar with codes of ethics and guidel...
متن کاملCT colonography: avoiding traps and pitfalls
Computed tomographic colonography (CTC) is a reliable technique for detecting tumoral lesions in the colon. However, good performance of polyp detection is only achieved if experienced CTC radiologists combine meticulous interpretation with state-of-the-art CTC technique. To reach this experience level, CTC training is mandatory. With a considerably long and steep learning curve, it has been de...
متن کاملTime‐lapse camera trapping as an alternative to pitfall trapping for estimating activity of leaf litter arthropods
Pitfall trapping is the standard technique to estimate activity and relative abundance of leaf litter arthropods. Pitfall trapping is not ideal for long-term sampling because it is lethal, labor-intensive, and may have taxonomic sampling biases. We test an alternative sampling method that can be left in place for several months at a time: verticallyplaced time-lapse camera traps that have a sho...
متن کاملذخیره در منابع من
با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید
عنوان ژورنال:
دوره شماره
صفحات -
تاریخ انتشار 1988